﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.IO;

namespace NewsMvc.Handler
{
    /// <summary>
    /// Summary description for UploadImage
    /// </summary>
    public class UploadImage : IHttpHandler
    {
        string _URLImageRoot = "Content/images/Upload/hide"; // get { return ConfigurationManager.AppSettings["ImageRootHide"]; }

        public void ProcessRequest(HttpContext context)
        {
            string CKEditorFuncNum = context.Request["CKEditorFuncNum"];
            string CKEditor = context.Request["CKEditor"];
            string langCode = context.Request["langCode"];
            string url;
            string message;
            try
            {
                if (context.Request.Files.Count == 0)
                {
                    context.Response.Write("<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"\", \"No file upload. Upload cancelled\");</script></body></html>");
                    context.Response.End();
                }
                HttpPostedFile upload = context.Request.Files[0];

                string path = upload.FileName.Replace(' ', '_');
                if (System.IO.File.Exists(context.Server.MapPath("~/" + _URLImageRoot + "/" + path)))
                    path = DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + path;
                if (!Directory.Exists(context.Server.MapPath("~/" + _URLImageRoot)))
                {
                    Directory.CreateDirectory(context.Server.MapPath("~/" + _URLImageRoot));
                }
                url = _URLImageRoot + "/" + path;
                var savePath = context.Server.MapPath("~/" + url);
                upload.SaveAs(savePath);
                message = "Image was saved correctly";
                string output = @"<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"" + url + "\", \"" + message + "\");</script></body></html>";
                context.Response.Write(output);
            }
            catch
            {
                context.Response.Write("<html><body><script>window.parent.CKEDITOR.tools.callFunction(" + CKEditorFuncNum + ", \"\", \"An Error occurred while uploading your file. Upload cancelled\");</script></body></html>");

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private bool IsImage(string file)
        {
            return file.EndsWith(".jpg", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".jpeg", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".jpe", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".bmp", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".tif", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".gif", StringComparison.CurrentCultureIgnoreCase) ||
                  file.EndsWith(".png", StringComparison.CurrentCultureIgnoreCase);
        }

    }

}